package com.flexdms.i18n.test.func;

import java.io.File;

import org.jboss.shrinkwrap.api.Archive;
import org.jboss.shrinkwrap.api.ArchivePath;
import org.jboss.shrinkwrap.api.Filter;
import org.jboss.shrinkwrap.api.GenericArchive;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.exporter.StreamExporter;
import org.jboss.shrinkwrap.api.exporter.ZipExporter;
import org.jboss.shrinkwrap.api.importer.ExplodedImporter;
import org.jboss.shrinkwrap.api.spec.JavaArchive;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.shrinkwrap.resolver.api.maven.Maven;
import org.jboss.shrinkwrap.resolver.api.maven.PomEquippedResolveStage;
import org.jboss.shrinkwrap.resolver.api.maven.archive.importer.MavenImporter;
import org.jboss.shrinkwrap.resolver.api.maven.strategy.MavenResolutionStrategy;

import com.sun.tools.javac.resources.compiler;

public class ArchiveUtil
{
	
	public static JavaArchive createI18NJar()
	{

		JavaArchive jar = ShrinkWrap.create(MavenImporter.class)
				.loadPomFromFile("pom.xml").importBuildOutput()
				.as(JavaArchive.class);

		System.out.println(jar.toString(true));
		return jar;
	}
	public static WebArchive addBasicLibs(WebArchive web)
	{
		PomEquippedResolveStage stage=Maven.resolver().loadPomFromFile("pom.xml");
		
		web.addAsLibraries(stage.resolve("com.google.http-client:google-http-client-jackson").withTransitivity().asFile());
		web.addAsLibraries(stage.resolve("org.apache.deltaspike.core:deltaspike-core-impl").withTransitivity().asFile());
		
		web.addAsLibraries(stage.resolve("org.omnifaces:omnifaces").withTransitivity().asFile());
		web.addAsLibraries(stage.resolve("asm:asm").withTransitivity().asFile());
		web.addAsLibraries(stage.resolve("com.google.apis:google-api-services-translate").withTransitivity().asFile());
		web.addAsLibraries(stage.resolve("net.sf.ehcache:ehcache").withTransitivity().asFile());
		
		web.addAsLibraries(stage.resolve("com.memetix:microsoft-translator-java-api").withTransitivity().asFile());
		//web.addAsLibraries(stage.resolve("com.google.apis:google-api-services-translate").withTransitivity().asFile());
		//web.addAsLibraries(stage.resolve("net.sf.ehcache:ehcache").withTransitivity().asFile());
		
		//web.addAsLibrary(new File("/home/jason/git/jsf2utils/target/jsfutils-1.1-SNAPSHOT.jar"));
		
		
		return web;
		
		
	}
	
	public static WebArchive populateWebInf(WebArchive web)
	{
		
		web.addAsWebInfResource("web.xml");
		web.addAsWebInfResource("glassfish-resources.xml");
		web.addAsWebInfResource("beans.xml");
		web.addAsWebInfResource("faces-config.xml");
		return web;
	}
	
	public static WebArchive buildWebArchive()
	{
		WebArchive web=ShrinkWrap.create(WebArchive.class, "i18n.war");
		web.addAsLibraries(createI18NJar());
		addBasicLibs(web);
		
		//test support classes
		web.addClass(TranslatorPorducer.class);
		web.addClass(FacesMessageer.class);
		//web.addClass(ZhCreator.class);
		populateWebInf(web);
		
		//translator configuration
		web.addAsResource("apache-deltaspike.properties", "META-INF/apache-deltaspike.properties");
		
		web.addAsWebResource("webapp/fm.xhtml", "fm.xhtml");
		web.addAsWebResource("webapp/text.xhtml", "text.xhtml");
		web.addAsWebResource("webapp/label.xhtml", "label.xhtml");
		web.addAsWebResource("webapp/editmsgs.xhtml", "editmsgs.xhtml");
		web.addAsWebResource("webapp/tempmsgs.xhtml", "tempmsgs.xhtml");
		
		System.out.println(web.toString(true));
		
		return web;
	}
	public static void main(String args[])
	{
		/*
		WebArchive web=buildWebArchive();
		web.as(ZipExporter.class).exportTo(new File("/home/jason/i18n.war"), true);
		*/
		System.out.println(createI18NJar());
	}

}
